package com.etboot.modules.activiti.serviceimpl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.etboot.modules.activiti.config.ActivitiConstant;
import com.etboot.modules.activiti.dao.mapper.ActNodeMapper;
import com.etboot.modules.activiti.entity.ActNode;
import com.etboot.modules.activiti.service.ActNodeService;
import com.etboot.modules.base.dao.DepartmentDao;
import com.etboot.modules.base.dao.RoleDao;
import com.etboot.modules.base.dao.mapper.UserMapper;
import com.etboot.modules.base.entity.Department;
import com.etboot.modules.base.entity.Role;
import com.etboot.modules.base.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 流程节点用户接口实现
 *
 * @author xboot
 */
@Slf4j
@Service
@Transactional
public class ActNodeServiceImpl extends ServiceImpl<ActNodeMapper, ActNode> implements ActNodeService {
    @Autowired
    private ActNodeMapper actNodeMapper;

    @Autowired
    // private UserDao userMapper;
    private UserMapper userMapper;

    @Autowired
    private RoleDao roleMapper;

    @Autowired
    private DepartmentDao departmentMapper;

    @Override
    public List<User> findUserByNodeId(String nodeId) {
        List<ActNode> listNode = actNodeMapper.selectList(Wrappers.<ActNode>lambdaQuery().eq(ActNode::getNodeId, nodeId).eq(ActNode::getType, ActivitiConstant.NODE_USER));
        // List<ActNode> listNode = actNodeDao.findByNodeIdAndType(nodeId, ActivitiConstant.NODE_USER);
        List<User> list = new ArrayList<>();
        listNode.forEach(e -> {
            User u = userMapper.selectById(e.getRelateId());
            if (u != null) {
                list.add(u);
            }
        });
        return list;
    }

    @Override
    public List<Role> findRoleByNodeId(String nodeId) {
        // List<ActNode> listNode = actNodeDao.findByNodeIdAndType(nodeId, ActivitiConstant.NODE_ROLE);
        List<ActNode> listNode = actNodeMapper.selectList(Wrappers.<ActNode>lambdaQuery().eq(ActNode::getNodeId, nodeId).eq(ActNode::getType, ActivitiConstant.NODE_ROLE));

        List<Role> list = new ArrayList<>();
        listNode.forEach(e -> {
            Role r = roleMapper.getOne(e.getRelateId()); // selectById
            // Role r = roleDao.findById(e.getRelateId()).orElse(null);
            if (r != null) {
                list.add(r);
            }
        });
        return list;
    }

    @Override
    public List<Department> findDepartmentByNodeId(String nodeId) {
        List<ActNode> listNode = actNodeMapper.selectList(Wrappers.<ActNode>lambdaQuery().eq(ActNode::getNodeId, nodeId).eq(ActNode::getType, ActivitiConstant.NODE_DEPARTMENT));
        // List<ActNode> listNode = actNodeDao.findByNodeIdAndType(nodeId, ActivitiConstant.NODE_DEPARTMENT);
        List<Department> list = new ArrayList<>();
        listNode.forEach(e -> {
            // Department d = departmentDao.findById(e.getRelateId()).orElse(null);
            Department d = departmentMapper.getOne(e.getRelateId());//selectById
            if (d != null) {
                list.add(d);
            }
        });
        return list;
    }

    @Override
    public List<String> findDepartmentIdsByNodeId(String nodeId) {
        List<ActNode> listNode = actNodeMapper.selectList(Wrappers.<ActNode>lambdaQuery().eq(ActNode::getNodeId, nodeId).eq(ActNode::getType, ActivitiConstant.NODE_DEPARTMENT));

        List<String> list = new ArrayList<>();
        listNode.forEach(e -> {
            list.add(e.getRelateId());
        });
        return list;
    }

    @Override
    public Boolean hasChooseDepHeader(String nodeId) {
//        List<ActNode> listNode = actNodeDao.findByNodeIdAndType(nodeId, ActivitiConstant.NODE_DEP_HEADER);
        List<ActNode> listNode = actNodeMapper.selectList(Wrappers.<ActNode>lambdaQuery().eq(ActNode::getNodeId, nodeId).eq(ActNode::getType, ActivitiConstant.NODE_DEP_HEADER));

        return listNode != null && listNode.size() > 0;
    }

    @Override
    public Boolean hasCustomUser(String nodeId) {
        List<ActNode> listNode = actNodeMapper.selectList(Wrappers.<ActNode>lambdaQuery().eq(ActNode::getNodeId, nodeId).eq(ActNode::getType, ActivitiConstant.NODE_CUSTOM));

        return listNode != null && listNode.size() > 0;
    }

    @Override
    public void deleteByNodeId(String nodeId) {
        actNodeMapper.delete(Wrappers.<ActNode>lambdaQuery().eq(ActNode::getNodeId, nodeId));
    }

}